<?php
	
	class EventUserBusiness {
		
		public function selectEventUserByEventId($eventId) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$eventUserDataAccess = DataAccessFactory::factory('EventUserDataAccessMySQL');
			
			$result = $eventUserDataAccess->selectEventUserByEventId($eventId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
		
		public function selectNotInvitedUserByEventId($eventId) {
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$eventUserDataAccess = DataAccessFactory::factory('EventUserDataAccessMySQL');
			
			$result = $eventUserDataAccess->selectNotInvitedUserByEventId($eventId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
		
		public function saveEventAttendant($event, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventUserDataAccess = DataAccessFactory::factory('EventUserDataAccessMySQL');
			
			$result = $eventUserDataAccess->saveEventAttendant($event, $dataBaseConnection);
			
			if ($connectionOpened) {
				if ($result == -1) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		public function updateEventAttendant($event, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventUserDataAccess = DataAccessFactory::factory('EventUserDataAccessMySQL');
			
			$result = $eventUserDataAccess->updateEventAttendant($event, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		public function deleteEventAttendant($event, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemBusiness = BusinessFactory::factory('EventItemBusiness');
			$result1 = $eventItemBusiness->deleteEventItemAttendantByRegistrationCode($event->getEventUser()->getUserRegistrationCode(), $dataBaseConnection);
			
			$eventUserDataAccess = DataAccessFactory::factory('EventUserDataAccessMySQL');
			$result2 = $eventUserDataAccess->deleteEventAttendant($event, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result1 || !$result2) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result2;
		}
		
	}
	
?>